package com.sj.manager.config;

import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.*;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
import org.springframework.web.servlet.view.JstlView;

/**
 * @desc:
 * @author: xsj
 * @date: 2022/3/10 11:21
 */
@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.sj.manager.controller" )
public class WebMvcConfig extends WebMvcConfigurerAdapter{

    ViewResolver viewResolver(){
        InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
        viewResolver.setPrefix("/WEB-INF/views/");
        viewResolver.setSuffix(".jsp");// 返回到页面的时候就会寻找  /WEB-INF/views/login.jsp
        viewResolver.setViewClass(JstlView.class);
        return viewResolver;
    }

    /**
     * 注册视图解析器
     * @param registry
     */
    @Override
    public void configureViewResolvers(ViewResolverRegistry registry) {
        registry.viewResolver(viewResolver());
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("login");//   /WEB-INF/views/index.jsp
    }

    /**
     * 配置静态资源的开启
     * @param configurer
     */
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/static/**").addResourceLocations("/WEB-INF/static/");
    }
}
